package com.project.projectdemo.utils;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UTFDataFormatException;
import java.io.UnsupportedEncodingException;
import java.net.BindException;
import java.net.ConnectException;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.sql.BatchUpdateException;
import java.sql.SQLException;
import java.sql.SQLTimeoutException;
import java.util.zip.DataFormatException;

/**
 * 统一异常类
 * @author MECHREVO
 *
 */
public class UnifiedException extends Exception {
	
	private static final long serialVersionUID = 1L;
	private String message;
	
	public UnifiedException(String message){
		super(message);
	}
	
	public UnifiedException(Exception e){
		if(e instanceof RuntimeException){
			//运行异常
			this.setRuntimeExceptionMessage((RuntimeException)e);
		}else if(e instanceof IOException){
			//IO异常
			this.setIOEceptionExceptionMessage((IOException)e);
		}else if(e instanceof SQLException){
			//数据库异常
			this.setSQLExceptionMessage((SQLException)e);
		}else if(e instanceof DataFormatException){
			//数据格式异常
			this.setDataFormatMessageException((DataFormatException)e);
		}else if(e instanceof ReflectiveOperationException){
			//核心反射中的反射操作所引起的异常的常见超类
			this.setReflectiveOperationExceptionMessage((ReflectiveOperationException)e);
		}else{
			this.setMessage("未知的异常信息！");
		}
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	//运行异常
	private void setRuntimeExceptionMessage(RuntimeException e){
		if(e instanceof NullPointerException){
			this.setMessage("空指针异常！");
		}else if(e instanceof NumberFormatException){
			this.setMessage("转换成数字的的字符串的格式有问题！");
		}else if(e instanceof ArithmeticException ){
			this.setMessage("算数计算出现问题！");
		}else if(e instanceof ArrayIndexOutOfBoundsException){
			this.setMessage("数组下标越界！");
		}else if(e instanceof ArrayStoreException){
			this.setMessage("组数元素的类型错误！");
		}else if(e instanceof StringIndexOutOfBoundsException){
			this.setMessage("字符串索引越界！");	
		}else if(e instanceof ClassCastException){
			this.setMessage("类型转换错误！");
		}else if(e instanceof SecurityException){
			this.setMessage("安全设置不符合要求！");
//		}else if(e instanceof NestedRuntimeException){
//			//mybatis所有异常的父类，没有使用mybatis框架时，去掉这里的代码
//			//TODO 待处理
//			this.setJDBCException((NestedRuntimeException) e);
		}else{
			this.setMessage(e.getMessage());
		}
	}
	//spring/mybatis数据库异常
//	private void setJDBCException(NestedRuntimeException e){
//		if(e instanceof MyBatisSystemException){
//			this.setMessage("数据库操作异常！");
//		}else if(e instanceof BadSqlGrammarException){
//			this.setMessage("sql语句执行有问题！");
//		}else{
//			this.setMessage("其他JDBC异常");
//		}
//	}
	
	//数据库异常
	private void setSQLExceptionMessage(SQLException e){
		if(e instanceof SQLTimeoutException){
			this.setMessage("数据库连接超时！");
		}else if(e instanceof BatchUpdateException){
			this.setMessage("数据库批量更新出现异常！");
		}else{
			this.setMessage("其他数据库异常！");
		}
	}
	
	
	//IO异常
	private void setIOEceptionExceptionMessage(IOException e){
		if(e instanceof SocketTimeoutException){
			this.setMessage("网络连接超时！");
		}else if(e instanceof BindException){
			this.setMessage("本地地址和端口连接失败！");
		}else if(e instanceof ConnectException){
			this.setMessage("远程地址和端口连接失败！");
		}else if(e instanceof MalformedURLException){	
			this.setMessage("URL有问题！");
		}else if(e instanceof FileNotFoundException){
			this.setMessage("读取的文件不存在！");
		}else if(e instanceof UTFDataFormatException){
			this.setMessage("UTF-8字符串的格式不正确！");
		}else if(e instanceof UnsupportedEncodingException){
			this.setMessage("不支持的字符编码！");
		}else{
			this.setMessage("其他输入输出流异常！");
		}
	}
	
	//数据格式异常
	@SuppressWarnings("unused")
	private void setDataFormatMessageException(DataFormatException e){
		//TODO 
		if(false){
			this.setMessage("待处理");
		}else{
			this.setMessage("数据格式异常！");
		}
	}
	
	//核心反射中的反射操作所引起的异常的常见超类
	private void setReflectiveOperationExceptionMessage(ReflectiveOperationException e){
		if(e instanceof ClassNotFoundException){
			this.setMessage("找不到指定的类！");
		}else if(e instanceof IllegalAccessException){
			this.setMessage("没有此方法或者此类的访问权限！");
		}else{
			this.setMessage("其他未知异常！");
		}
	}
	
	
}
